import Vue from 'vue'
import Cookies from 'js-cookie';
//全局引入混用
Vue.mixin({
    methods:{
        //全局请求接口
        getJsonData(methods,obj,isCatch){
            let params=obj?obj:{}
            params.token=Cookies.get("token")
            return new Promise((resolve,reject) => {
                this.$http.get(this.ajxaHttpUrl+methods,{ params:params })
                    .then((res) => {
                        if(res.data.status==1){
                            this.$Loading.finish();
                            resolve(res.data)
                        }else{
                            this.$Loading.error();
                            setTimeout(()=>{
                                this.$Message.error(res.data.msg||res.data.info);
                            },500)
                            if(isCatch)reject(res.data.msg || res.data.info)
                        }
                    })
                    .catch((err) => {
                        this.$Loading.error();
                        setTimeout(()=>{
                            this.$Message.error(err);
                        },500)
                        if(isCatch)reject(err)
                    });
            })
        },
        //formData
        sendFormData(methods,obj,isCatch){

            // obj.token=Cookies.get("token")
            // console.log(obj instanceof FormData);
            obj.append('token',Cookies.get('token'));
            // console.log(obj)
            // let params=obj?obj:{}

            // params.token=Cookies.get("token")
            return new Promise((resolve,reject) => {
                this.$http.post(this.ajxaHttpUrl+methods,{ data:obj })
                    .then((res) => {

                        if(res.data.status==1){
                            this.$Message.success(res.data.msg||res.data.info);
                            this.$Loading.finish();
                            resolve(res.data)
                        }else{
                            this.$Loading.error();
                            setTimeout(()=>{
                                this.$Message.error(res.data.msg||res.data.info);
                            },500)
                            if(isCatch)reject(res.data.msg||res.data.info)
                        }
                    })
                    .catch((err) => {
                        this.$Loading.error();
                        setTimeout(()=>{
                            this.$Message.error(err);
                        },500)
                        if(isCatch)reject(err)
                    });
            })
        },
        //上传图片
        upBaseImg(methods,obj){
            let params=obj
            params.token=Cookies.get("token")
            return new Promise((resolve,reject) => {
                this.$http.post(this.ajxaHttpUrl+methods,params).then((res) => {
                        if(res.data.status==1){
                            resolve(res.data)
                            this.$Loading.finish();
                        }else{
                            this.$Loading.error();
                            setTimeout(()=>{
                                this.$Message.error(res.data.msg || res.data.info );
                            },500)
                        }
                    })
                    .catch((err) => {
                        this.$Loading.error();
                        setTimeout(()=>{
                            this.$Message.error(err);
                        },500)
                    });
            })
        },
        timeFormat(n){
            var date = new Date(n*1000);
            var year = date.getFullYear();
            var month = (date.getMonth() + 1)<10?'0'+(date.getMonth() + 1):(date.getMonth() + 1);
            var day = date.getDate()<10?'0'+date.getDate():date.getDate();
            var hours = date.getHours()<10?'0'+date.getHours():date.getHours();
            var minute = date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes();
            var second = date.getSeconds()<10?'0'+date.getSeconds():date.getSeconds();
            return year+'-'+month+'-'+day+' '+hours+':'+minute+":"+second
        },
        timeFormatOne(n){
            var date = new Date(n*1000);
            var year = date.getFullYear();
            var month = (date.getMonth() + 1)<10?'0'+(date.getMonth() + 1):(date.getMonth() + 1);
            var day = date.getDate()<10?'0'+date.getDate():date.getDate();
            return year+'-'+month+'-'+day
        }
    }
})
